package com.reeygu.item.validation.annotationActivate;

import com.reeygu.item.utils.CharacterUtils;
import com.reeygu.item.validation.IsMobile;
import org.thymeleaf.util.StringUtils;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class IsMobileValidation implements ConstraintValidator<IsMobile, String> {
    private boolean required = false;

    @Override
    public void initialize(IsMobile constraintAnnotation) {
        required = constraintAnnotation.required();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        //非必填
        if(!required && StringUtils.isEmpty(value)) {
            //空字符串直接放行
            return true;
        }
        return CharacterUtils.isPhone(value);
    }
}
